<?php

class userController extends baseController
{
	public function show()
	{
		$this->assign('list',	User::getAll());
		$this->assign('opt', array(array('修改', 'javascript:modify(%d)')));
		$this->display('user-show');
	}

	/**
	 * 开关用户状态
	 */
	public function disa()
	{
		$uid	= fetch_get('user_id', 'int');
		$disa	= fetch_get('disa', 'int');
		if(!$uid) {
			Show::warning("参数错误");
		}

		$disa	= $disa?0:1;
		if(!User::update($uid, array('is_disa'=>$disa))) {
			Show::warning("修改失败!");
		}

		Show::ajaxBand(array(
			'user_id'	=>	$uid,
			'disa'		=>	$disa,
		));
		LoggerHelper::manage("设置用户状态为'{$disa}'，UID/ID:{$uid}");
		Show::notice("修改用户状态成功");
	}
	
	/**
	 * 增加和修改
	 */
	public function save()
	{
		$user_id	= fetch_post('user_id', 'int');

		if(!$user_id && !fetch_post('name')) {
			Show::warning("请输入正确的用户名");
		}
		if(!fetch_post('dept_id', 'int')) {
			Show::warning("请选择正确的用户组");
		}

		if(!$user_id)
		{
			$account = fetch_post('account');
			if(!$account || User::getByAccount($account)){
				Show::warning('账号错误或已使用');
			}
			$user_id	= UserHelper::add(array(
					'account'	=> $account,
					'name'	=> fetch_post('name'),
					'passwd'	=> fetch_post('passwd','char'),
					'dept_id'	=> fetch_post('dept_id'),
					'is_disa'	=> 0,
					'email'		=> fetch_post('email'),
                                        'mobile'	=> fetch_post('mobile'),
                                        'remark'	=> fetch_post('remark')
                            ));
			LoggerHelper::manage("增加用户记录，用户名'{$account}'、姓名'".fetch_a_string('name')."'，ID:{$user_id}");
			Show::ajaxBand(array('method'=>'insert'));
		}
		else
		{
			UserHelper::modify($user_id, array(
					'passwd'	=> fetch_post('passwd','char'),
					'name'		=> fetch_post('name'),
					'dept_id'	=> fetch_post('dept_id'),
					'email'		=> fetch_post('email'),                            
                                        'mobile'	=> fetch_post('mobile'),
                                        'remark'	=> fetch_post('remark')));
			CheckinHelper::trackPasswd($user_id, fetch_post('passwd','char'));
			LoggerHelper::manage("编辑用户记录，姓名'".fetch_a_string('name')."'，ID:{$user_id}");
			Show::ajaxBand(array('method'=>'update'));
		}

		$this->assign('depts',	Dept::getList());
		$this->assign('u', User::get($user_id));
		$html = $this->fetch('user-saved');

		Show::ajaxBand(array(
			'html'		=>	$html,
			'user_id'	=>	$user_id,
		));
		Show::notice("保存成功！");
	}

	/**
	  添加修改用户
	  */
	public function nature()
	{
		if($user_id=fetch_get('user_id', 'int')){
			if(!($user=User::get($user_id))){
				Show::ajaxLeave('fail', 'No such user!');
			}
			else{
				$this->assign('user', $user);
			}
		}
		$this->assign('depts',	Dept::getList());
		Show::ajaxLeave('ok', $this->fetch());
	}

	public function role() {
		$id	= fetch_a_int('id');
		if(!$id) {
			$this->assign('page_title2', '用户权限');
			$this->assign('list', User::getAll());
			$this->display('user-show');
			return ;
		}

		$user	= User::get($id);
		if(!$user) {
			Show::warn("无此用户", 'user');
		}

		if(is_post()) {
			UserHelper::purgeRoles($id);
			UserHelper::assumeRole($id, fetch_a_array('role'));
			LoggerHelper::manage("编辑用户授权，ID:{$id}");
			Show::note("授权成功");
		}

		$roles	= UserHelper::getRoles($id);
		$user['roles']	= $roles;
		
		$this->assign('user',	$user);
		$this->assign('roles',	Role::getPureList());
		
//		Debug::pre($user);
//		Debug::pre(Role::getPureList());
		$this->display();
	}

	public function roles(){
		$list = UserHelper::getListWithPrivs();
		Debug::pre($list);
	}

	/**
	 * 删除
	 */
	public function delete()
	{
		if(!fetch_get('user_id', 'int')) {
			Show::warn("参数错误");
		}
		if(RoleHelper::delete(fetch_get('user_id', 'int'))) {
			LoggerHelper::manage("删除用户记录，ID:".fetch_get('user_id', 'int'));
			Show::notice("删除成功");
		}
		else {
			Show::warning("删除失败");
		}
	}
	
}

?>
